/**
 * Copyright (C) 2010 - 2013 Harry Glasgow
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.jaden.ui;

import com.googlecode.jaden.common.other.Utils;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class UiUtils {

    private static final Cursor WAIT_CURSOR = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
    private static final Cursor HAND_CURSOR = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
    private static final Cursor DEFAULT_CURSOR = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);

    public static JButton createButton(String key) {
        return new JButton(Utils.translate(key));
    }

    public static JPanel horizontalList(int justify, JComponent... components) {
        JPanel panel = new JPanel(new FlowLayout(justify));
        for (JComponent component : components) {
            panel.add(component);
        }
        return panel;
    }

    public static JPanel horizontalList(JComponent... components) {
        return horizontalList(FlowLayout.LEFT, components);
    }

    public static JPanel verticalList(JComponent... components) {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        for (JComponent component : components) {
            panel.add(component);
        }
        return panel;
    }

    public static void setWaitCursor(JComponent component) {
        RootPaneContainer root = (RootPaneContainer) component.getTopLevelAncestor();
        root.getGlassPane().setCursor(WAIT_CURSOR);
        root.getGlassPane().setVisible(true);
    }

    public static void clearCursor(JComponent component) {
        RootPaneContainer root = (RootPaneContainer) component.getTopLevelAncestor();
        root.getGlassPane().setVisible(false);
        root.getGlassPane().setCursor(DEFAULT_CURSOR);
    }

    public static void setHandCursor(JComponent component) {
        RootPaneContainer root = (RootPaneContainer) component.getTopLevelAncestor();
        root.getGlassPane().setCursor(HAND_CURSOR);
        root.getGlassPane().setVisible(true);
    }

    public static void enWidth(JComponent... components) {
        int width = 0;
        for (JComponent component : components) {
            width = Math.max((int) component.getPreferredSize().getWidth(), width);
        }
        for (JComponent component : components) {
            component.setPreferredSize(new Dimension(width, (int) component.getPreferredSize().getHeight()));
        }
    }

    public static ImageIcon getImageIcon(String fileName) {
        return new ImageIcon(getImage(fileName));
    }

    public static Image getImage(String fileName) {
        URL imageURL = Thread.currentThread().getContextClassLoader().getResource(fileName);
        return Toolkit.getDefaultToolkit().getImage(imageURL);
    }
}
